/**
 * Main UI thread
 * Purpose	creates game logic thread and OpenGL thread
 */
package com.unknown.explorer;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;

import com.threed.jpct.Logger;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.text.Layout;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements Handler.Callback, OnSeekBarChangeListener{

	//other threads/tasks 
//	private renderTaskOpenGL renderer = null;
	
	//private GLSurfaceView mGLView = null;
	private myGLSurfaceView myGLView;
	
	private gameLogic myGameLogic;
	
	private Handler h_ui;
	
	private Handler h_gameLogic;
	
	private TextView myText;
	
	private int loopNum = 0;
	
	private boolean bypass = false;
	
	Context myAppContext;
	Context mGLViewContext;
	
	private SeekBar bt;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
       
    	Logger.log("onCreate");
    	
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        View bottomPanel = this.findViewById(R.id.botLinearLayout);
        
        //already placed in the xml layout
        //mGLView = new GLSurfaceView(this.getApplication());

        myGLView = (myGLSurfaceView) this.findViewById(R.id.myGLSurfaceView1);
        
//        myGLView = new myGLSurfaceView(this.getApplication());
        
        myGLView.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() {
    		public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
    			// Ensure that we get a 16bit framebuffer. Otherwise, we'll fall
    			// back to Pixelflinger on some device (read: Samsung I7500)
    			int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
    			EGLConfig[] configs = new EGLConfig[1];
    			int[] result = new int[1];
    			egl.eglChooseConfig(display, attributes, configs, 1, result);
    			return configs[0];
    		}
    	});
        
    	mGLViewContext = myGLView.getContext();
    	myAppContext = mGLViewContext.getApplicationContext();
    
    	myGameLogic = new gameLogic(h_ui, this);
    //	renderer = new renderTaskOpenGL(myAppContext);
    	myGLView.setRenderer(myGameLogic);
    	
    	myGLView.setHandlerGameLogic(myGameLogic.getHandlerGameLogic());
    	
    //already placed in the xml layout
    //	setContentView(myGLView);
        
      //  myText = (TextView) findViewById(R.id.textView1);
      //  myText.setText("newly created text"); 
   
      //  Handler h_ui = new Handler(this);
        
//        myGameLogic = new gameLogic(h_ui);
//        myGameLogic.setHandler3D(renderer.h_3D);
        
    	h_gameLogic = myGameLogic.getHandlerGameLogic();
    	
    	myGameLogic.start();
        
        bt = (SeekBar) findViewById(R.id.rotateBar);
        bt.setAlpha(0.5f);
        bt.setMax(720);
        bt.setProgress(360);
        bt.setOnSeekBarChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    /**
     * Message processing from game logic thread
     */
	public boolean handleMessage(Message msg) {
		myText.setText(String.valueOf(msg.arg1));
		
		loopNum++;
		
		Handler otherHandler = myGameLogic.h_gameLogic;
		Message m = new Message();
		m.arg1 = 3;
		otherHandler.sendMessage(m);
		
		return false;
	}

	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		Logger.log("Progress: " + progress);
		
		gameMessage sentMsg = new gameMessage();
		sentMsg.messageType = "camera rotate";
		sentMsg.myData.put("camera angle", (float)progress);
		
		Message m = new Message();
		m.obj = sentMsg;
		
		h_gameLogic.sendMessage(m);
		
	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	};
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		  super.onSaveInstanceState(savedInstanceState);
	}
   
}
